package net.wantongtech.xa.uploadbasicinfo.service;

import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wtkj.vo.dzzf.base.qxsjz.entity.PortalTransOut;
import com.wtkj.vo.dzzf.share.staticvalue.CardStaticValue;
import com.wtkj.zwxt.enums.SignSourceEnum;
import lombok.extern.slf4j.Slf4j;
import net.wantongtech.xa.uploadbasicinfo.config.TollBasicInfoConfig;
import net.wantongtech.xa.uploadbasicinfo.dao.AlterChannelTypeDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import static com.wtkj.constant.NewClearingConstant.*;
import static com.wtkj.vo.dzzf.share.staticvalue.CardStaticValue.EcardType.CARD_TYPE_JZ;

/**
 * 修改channelType服务实现
 *
 * @author MaShibo
 * 日期 :  2020/1/8
 */
@Slf4j
@Service
public class AlterChannelTypeService {

    @Resource
    private AlterChannelTypeDao alterChannelTypeDao;

    @Resource
    private TollBasicInfoConfig tollBasicInfoConfig;

    public void process() {

        log.info("开始处理...");
        List<PortalTransOut> portalTransOutList = alterChannelTypeDao.queryErrorPortalTransoutInfo();
        log.info("查询到{}条无channelType的待处理数据", portalTransOutList.size());
        if (portalTransOutList.isEmpty()) {
            log.info("执行结束");
            return;
        }
        portalTransOutList.forEach(portalTransOut -> {

            try {
                processSingle(portalTransOut);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }

        });
    }


    private void processSingle(PortalTransOut portalTransOut) {
        String etcCardId = portalTransOut.getEtcCardId();
        log.info("开始处理卡号为【{}】的数据...", etcCardId);

        String obuId = portalTransOut.getObuId();
        String issuerId = portalTransOut.getIssuerId();
        Integer etcCardType = portalTransOut.getEtcCardType();

        //若卡号不为空则查询并填充卡片信息
        boolean hasCardId = StringUtils.isNotBlank(etcCardId);

        if (!hasCardId && StringUtils.isNotBlank(obuId)) {
            Optional<String> optional = alterChannelTypeDao.queryCardNoByObuId(obuId);
            etcCardId = optional.orElse(null);
            hasCardId = StringUtils.isNotBlank(etcCardId);
        }

        String channelType = null;
        String signSource = null;
        if (hasCardId) {

            String cardNetId = etcCardId.substring(0, 4);
            String eCardNo = etcCardId.substring(4);
            CardInfo cardInfo = alterChannelTypeDao.queryCardInfoByNo(eCardNo);

            ///6101记账卡从cardInfo中获取对应的渠道信息
            if (JT_CARD_NET_ID.equals(cardNetId)) {
                if (cardInfo != null && cardInfo.getECardType() == CARD_TYPE_JZ) {
                    channelType = cardInfo.getChannelType();
                    signSource = cardInfo.getSignSource();
                }
                // 6107确定渠道信息
            } else if (SIX_BANK_NET_ID.equals(cardNetId)) {
                String sectionNo = etcCardId.substring(11, 13);
                channelType = tollBasicInfoConfig.getChannelTypeBySectionNo(sectionNo);
                //无法确定渠道信息的都为"光大"
                if (channelType == null) {
                    channelType = CEB_CHANNEL_TYPE;
                }
                signSource = SignSourceEnum.SELF_PUBLISH.getSource();
            } else {
                channelType = tollBasicInfoConfig.getChannelTypeByCardNetId(cardNetId);
                signSource = SignSourceEnum.SELF_PUBLISH.getSource();
            }
        }

        // 6102 - 6106确定渠道信息
        if (Stream.of(JT_CARD_NET_ID, SIX_BANK_NET_ID).noneMatch(v -> v.equals(issuerId))
                && channelType == null) {
            channelType = tollBasicInfoConfig.getChannelTypeByIssuerId(issuerId);
            signSource = SignSourceEnum.SELF_PUBLISH.getSource();
        }


        Integer parseCardType = null;
        if (etcCardType == null && hasCardId) {
            if (etcCardId.length() == 20) {
                int sectionNo = Integer.parseInt(etcCardId.substring(8, 10));
                if (sectionNo != CardStaticValue.EcardType.CARD_TYPE_JZ) {
                    parseCardType = CardStaticValue.EcardType.CARD_TYPE_CZ;
                } else {
                    parseCardType = sectionNo;
                }
            } else {
                parseCardType = CardStaticValue.EcardType.CARD_TYPE_JZ;
            }
        }

        log.info("待更新的channelType：{}，signSource：{}，etcCardType：{}", channelType, signSource, parseCardType);
        PortalTransOut update = new PortalTransOut();
        update.setTransactionId(portalTransOut.getTransactionId());
        update.setChannelType(channelType);
        update.setSignSource(signSource);
        update.setEtcCardType(parseCardType);
        alterChannelTypeDao.updateObject(update);
    }
}
